package com.zelin.page.web.listener;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zelin.entity.vo.PayAsyncVo;
import com.zelin.pay.service.PayService;
// import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/8/17-16:44
 * ------------------------------
 */
@RestController
public class PaySyncController {

    @Reference(timeout = 5000)
    private PayService payService;

    /**
     * 功能: 支付宝发送异步通知【订单支付模块】
     * 参数:
     * 返回值: java.lang.String
     * 时间: 2021/8/17 16:46
     */
    @PostMapping("/payed/notify")
    public String payed(PayAsyncVo vo, HttpServletRequest request){
        System.out.println("vo = " + vo);
        //1. 得到登录用户
        //String userId = SecurityContextHolder.getContext().getAuthentication().getName();
        //2. 得支支付宝后台传入的数据
        Map<String,String> params = new HashMap<>();
        Map<String,String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用
            //valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }

        String result = payService.payNotify(vo,params);
        System.out.println("result = " + result);
        return result;

    }

    /**
     * 功能: 支付宝发送异步通知【秒杀支付模块】
     * 参数:
     * 返回值: java.lang.String
     * 时间: 2021/8/17 16:46
     */
    @PostMapping("/seckillOrder/payed/notify")
    public String payedNotify(PayAsyncVo vo, HttpServletRequest request){
        System.out.println("vo = " + vo);
        //1. 得到登录用户
        //String userId = SecurityContextHolder.getContext().getAuthentication().getName();
        //2. 得支支付宝后台传入的数据
        Map<String,String> params = new HashMap<>();
        Map<String,String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用
            //valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }

        String result = payService.orderPayNotify(vo,params);
        System.out.println("result = " + result);
        return result;

    }


}
